package com.wlper.lib.view.loading;

import com.android.nearshop.R;


import com.wlper.lib.uitls.ViewUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

/**
 * 采用Tween动画
 * 
 * @author wanliang527
 * 
 */
public class LoadLayoutWithTweenAnim extends SimpleLoadLayout {
    private Animation rotateAnim;

    public LoadLayoutWithTweenAnim(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public LoadLayoutWithTweenAnim(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoadLayoutWithTweenAnim(Context context) {
        super(context);
    }

    @Override
    protected void startLoading() {
        changeClickable(false);
        loadFailedView.setVisibility(View.GONE);
        loadingView.setVisibility(View.VISIBLE);
        ImageView v = (ImageView) findViewById(R.id.image_v_loading);
        if (v.getBackground() == null)
            v.setBackgroundResource(R.drawable.wl_ic_loading);
        if (rotateAnim == null) {
            rotateAnim = AnimationUtils.loadAnimation(mContext,
                    R.anim.wl_image_rotate_anim);
        }
        v.clearAnimation();
        v.setAnimation(rotateAnim);
    }

    @Override
    protected void stopLoading(boolean isSuccess) {
        ImageView v = (ImageView) findViewById(R.id.image_v_loading);
        if (v != null)
            v.clearAnimation();
        loadingView.setVisibility(View.GONE);
        changeClickable(false);
        if (!isSuccess) {
            loadFailedView.setVisibility(View.VISIBLE);
            changeClickable(true);
        }
    }

    public void setLoadingDrawable(Object obj) {
        ImageView i = (ImageView) findViewById(R.id.image_v_loading);
        if (obj == null || i == null)
            return;
        i.clearAnimation();
        ViewUtils.setViewBackground(i, obj);
        if (rotateAnim == null) {
            rotateAnim = AnimationUtils.loadAnimation(mContext,
                    R.anim.wl_image_rotate_anim);
        }
        i.setAnimation(rotateAnim);
    }

}
